# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#    MA  02110-1301  USA

import msgWin
import curses

class LstWin(msgWin.MsgWin):
	def __init__(self, mam, title = '', msg = [], buttons = [], width = 50, lstLines = []):
		self.mam = mam
		self.mam.ui.options_win = 1
		self.title = title
		self.msg = msg
		self.buttons = buttons
		self.width = width
		self.lstLines = lstLines
		self.buttonIndex = self.defaultButIndex()
		self.visibleLstLines = self.mam.ui.maxY/2-2
		self.lstLineIndex = 0
		self.height = self.calculateHeight()
		# Creo la MsgWin
		msgWin.MsgWin.__init__(self, self.mam, self.title, self.msg, self.buttons, self.width)
		# Muestro la ventana
		self.show()
		self.mam.ui.winOpt2 = self

	def maxLenLstLine(self):
		max = 0
		for l in self.lstLines:
			if len(l) > max:
				max = len(l)
		return max

	def calculateHeight(self):
		height = 6
		for line in self.msg:
			if line.nl: 
				height += 1
		if len(self.msg):
			height += 1
		else:
			height -= 1
		# only the following lstLines visible
		height += min(self.visibleLstLines, len(self.lstLines))+1
		return height

	def rebuildWin(self):
		self.show()

	def manageInput(self, carac):
		# F4
		if carac == int(0x10c):
			return -1, ''
		# ENTER
		elif carac == int(0xa):
			return self.buttonIndex, self.lstLineIndex
		# TAB
		elif carac == int(0x9):
			self.buttonIndex += 1
			if self.buttonIndex == len(self.buttons):
				self.buttonIndex = 0
		# KEY_DOWN
		elif carac == curses.KEY_DOWN:
			if self.lstLineIndex <> len(self.lstLines) - 1:
				self.lstLineIndex += 1
		# KEY_UP
		elif carac == curses.KEY_UP:
			if self.lstLineIndex <> 0:
				self.lstLineIndex -= 1
		# KEY_HOME
		elif carac == curses.KEY_HOME:
			self.lstLineIndex = 0
		# KEY_END
		elif carac == curses.KEY_END:
			self.lstLineIndex = len(self.lstLines) - 1
		# PAGE UP
		elif carac == curses.KEY_PPAGE:
			o = self.lstLineIndex
			n = self.visibleLstLines
			if o-o%n >= n:
				self.lstLineIndex -= o%n + 1
		# PAGE DOWN
		elif carac == curses.KEY_NPAGE:
			o = self.lstLineIndex
			n = self.visibleLstLines
			l = len(self.lstLines)
			if o+n-o%n < l:
				self.lstLineIndex += n-o%n
		self.refresh()
		return -2, ''

	def show(self):
		self.w.bkgd(self.mam.ui.COLORS.OPTIONS_WIN1)
		# Creo el borde
		self.mam.ui.rectangle(self.w, 0, 0, self.height - 1, self.width - 1)
		# Muestro title
		self.move(1,3)
		self.addstr(self.title, self.mam.ui.COLORS.OPTIONS_WIN2)
		# Muestro mensaje/s
		i = 3
		lineChars = 0
		for line in self.msg:
			if lineChars:
				lineChars += 1
			self.move(i, max(lineChars + (self.width - lineChars + 1 - len(line.text))/2, 3))
			self.addstr(line.text[:self.width-6], line.color)
			if line.nl: 
				i += 1
				lineChars = 0
			else:
				lineChars += len(line.text)
		self.refresh()

	def refresh(self):
		# Muestro botones
		colorDef = self.mam.ui.COLORS.OPTIONS_WIN5
		colorNotDef = self.mam.ui.COLORS.OPTIONS_WIN1
		numBut = 0
		for but in self.buttons:
			self.move(self.height - 2, (self.width-self.lenButtons())/2 + numBut*9)
			numBut += 1
			if self.buttons.index(but) == self.buttonIndex:
				self.addstr(" " + but.descrip + " ", colorDef)
			else:
				self.addstr(" " + but.descrip + " ", colorNotDef)

		# Refresh de las lstLines
		i = 0
		n = min(self.visibleLstLines, len(self.lstLines))
		o = self.lstLineIndex
		for i in range(n):
			self.move(i+(self.height - n - 3), 3)
			# Clean row
			for j in range(self.width-4):
				self.addstr(" ")
			# Show the lstLine
			self.move(i+(self.height - n - 3), 3)
			if i == o%n:
				color = self.mam.ui.COLORS.OPTIONS_WIN5
			else:
				color = self.mam.ui.COLORS.OPTIONS_WIN1
			if o/n*n+i < len(self.lstLines):
				self.addstr(self.lstLines[o/n*n+i], color)
				# Complete til end of line in the selected one
				for l in range(self.width-6-len(self.lstLines[o/n*n+i])):
					self.addstr(" ", color)
		# Refresh de ventana
		self.w.refresh()
